package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.BpmnInfo;
import cn.wolfcode.car.business.domain.CarPackageAudit;
import cn.wolfcode.car.business.domain.ServiceItem;
import cn.wolfcode.car.business.mapper.BpmnInfoMapper;
import cn.wolfcode.car.business.mapper.CarPackageAuditMapper;
import cn.wolfcode.car.business.mapper.ServiceItemMapper;
import cn.wolfcode.car.business.query.ServiceItemQuery;
import cn.wolfcode.car.business.service.IServiceItemService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ServiceItemServiceImpl implements IServiceItemService {

    @Autowired
    private ServiceItemMapper serviceItemMapper;
    @Autowired
    private BpmnInfoMapper bpmnInfoMapper;
    @Autowired
    private CarPackageAuditMapper carPackageAuditMapper;
    @Autowired
    private RuntimeService runtimeService;


    @Override
    public TablePageInfo<ServiceItem> query(ServiceItemQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<ServiceItem>(serviceItemMapper.selectForList(qo));
    }


    @Override
    public void save(ServiceItem serviceItem) {
        serviceItem.setCreateTime(new Date());  //设置当前时间默认值 传入date
        serviceItem.setSaleStatus(ServiceItem.SALESTATUS_OFF);  //是否上下架 默认为下架
        if (ServiceItem.CARPACKAGE_YES.equals(serviceItem.getCarPackage())){  //如果是套餐
            serviceItem.setAuditStatus(ServiceItem.AUDITSTATUS_INIT);        //审核状态设置为初始化
        }else {
            serviceItem.setAuditStatus(ServiceItem.AUDITSTATUS_NO_REQUIRED);  //如果不是套餐 无需审核
        }

        serviceItemMapper.insert(serviceItem);
    }

    @Override
    public ServiceItem get(Long id) {
        return serviceItemMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(ServiceItem serviceItem) {
        ServiceItem serviceItem2 = this.get(serviceItem.getId());
        if (ServiceItem.SALESTATUS_ON.equals(serviceItem2.getSaleStatus())){
            throw new BusinessException("上架服务项目不能修改，请下架后再修改");
        }else if (ServiceItem.AUDITSTATUS_AUDITING.equals(serviceItem2.getCarPackage())){
            throw new BusinessException("服务项目正在审核中,不可修改");
        }

        serviceItemMapper.updateByPrimaryKey(serviceItem);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            serviceItemMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<ServiceItem> list() {
        return serviceItemMapper.selectAll();
    }

    @Override
    public void saleoff(Long id) {
        ServiceItem serviceItem = serviceItemMapper.selectByPrimaryKey(id);

        Integer carPackage = serviceItem.getCarPackage();
        if (carPackage.equals(ServiceItem.CARPACKAGE_YES)){ // 准备下架 如果是套餐
            serviceItemMapper.changeSaleStatus(ServiceItem.AUDITSTATUS_INIT,id); //把审核状态改为初始化

        }

        Integer salestatusOff = ServiceItem.SALESTATUS_OFF;  // 当上架状态位上架时 改为下架

        serviceItemMapper.changeSaleStatus(salestatusOff,id);
    }

    @Override
    public void saleOn(Long id) {
        ServiceItem serviceItem = serviceItemMapper.selectByPrimaryKey(id);

        Integer carPackage = serviceItem.getCarPackage();
        if (carPackage.equals(ServiceItem.CARPACKAGE_YES)){
            serviceItemMapper.changeSaleStatus(ServiceItem.AUDITSTATUS_APPROVED,id);
        }
        Integer salestatusOn = ServiceItem.SALESTATUS_ON;
        serviceItemMapper.changeSaleStatus(salestatusOn,id);
    }

    @Override
    public void startAudit(Long id, Long bpmnInfoId, Long finance, Long director, String info) {
        ServiceItem serviceItem = this.get(id);
        if (! ServiceItem.SALESTATUS_OFF.equals(serviceItem.getSaleStatus()) ||
            ! ServiceItem.AUDITSTATUS_INIT.equals(serviceItem.getAuditStatus()) ||
            ! ServiceItem.CARPACKAGE_YES.equals(serviceItem.getCarPackage())){
            throw new BusinessException("状态不符合");
        }
        //把审核状态改为审核中
        serviceItemMapper.changeAuditStatus(ServiceItem.AUDITSTATUS_AUDITING,id);



        CarPackageAudit audit = new CarPackageAudit();
        audit.setServiceItemId(id);
        audit.setServiceItemInfo(serviceItem.getInfo());
        audit.setServiceItemPrice(serviceItem.getDiscountPrice());

        audit.setCreator(ShiroUtils.getUser().getUserName());//登录名称和user名称区别???
        audit.setBpmnInfoId(bpmnInfoId);
        audit.setInfo(info);
        audit.setCreateTime(new Date());
        audit.setAuditorId(director);
        carPackageAuditMapper.insert(audit); //审核信息记录表

        Map<String,Object> map = new HashMap<>();
        if (director != null){
            map.put("director",director.toString());
        }

        if (finance != null){
            map.put("finance",finance.toString());
        }
        map.put("discountPrice",serviceItem.getDiscountPrice().longValue());
        BpmnInfo bpmnInfo = bpmnInfoMapper.selectByPrimaryKey(bpmnInfoId);

        ProcessInstance instance = runtimeService.startProcessInstanceByKey(
                bpmnInfo.getActProcessKey(), audit.getId().toString(), map);

        audit.setInstanceId(instance.getId());

        carPackageAuditMapper.updateByPrimaryKey(audit);

    }

    @Override

    public void changeAuditStatus(Long serviceItemId, Integer status) {
        serviceItemMapper.changeAuditStatus(status, serviceItemId);
    }


}
